<?php
include(dirname(__FILE__).'/../../../includes.php5');
header('Content-Type: text/html; charset=UTF-8');

include_once($project_path.'/class/user/user.class.php5');
session_start();
include_once($project_path.'/class/fw/dictionary.class.php5');

userUser::checkSession();

$action=(isset($_GET['action'])?$_GET['action']:'');
switch($action)
{
	case 'check':
		if($_SESSION['current_user']->isRoot())
		{
			include($project_path.'/install_report.php5');
			//@todo attention au depassement des 30 secondes dans le cas ou il y a pls hosts
			$_SESSION['install']['packages'] = array();
			foreach($conf['appli']['packages'] as $application_name => $application_data)
			{
				$xml = file_get_contents($application_data['host'].'index.php?action=info_version&application='.$application_name);
				if($xml!='')
				{
					$data = @new SimpleXMLElement($xml);
					''.$data->application[0]->version[0];
					if($application_name == ''.$data->application[0]->name[0])
					{
						if(isset($application_data['update_version']) && $application_data['update_version'] == ''.$data->application[0]->version[0])
						{
							if($application_data['update_action'] == 0)
							{
								continue;
							}
						}
						$conf['appli']['packages'][$application_name]['update_version'] = ''.$data->application[0]->version[0];
						$conf['appli']['packages'][$application_name]['update_action'] = 2;
						fwTools::saveConfig('appli');
						if(fwTools::compareVersions($application_data['version'], ''.$data->application[0]->version[0])<0)
						{
							$_SESSION['install']['packages'][] = array(
								'host' => $application_data['host'],
								'name' => $application_name,
								'old_version' => $application_data['version'],
								'new_version' => ''.$data->application[0]->version[0]
							);
						}
					}
				}
			}
			if(count($_SESSION['install']['packages'])>0)
			{
				include_once($project_path.'/class/fw/smarty.class.php5');
				$smarty = new fwSmarty();
				$smarty->assign('packages', $_SESSION['install']['packages']);

				echo 'update_popup = new Popup(360, 300);
update_popup.showModal("'.fwConverter::text2JSString($smarty->getContents('admin/update.tpl')).'");
';
			}
		}
		break;
	case 'progress':
		echo 'updateProgressReponse=['.((intval($_SESSION['install']['progress']))/10).', "'.$_SESSION['install']['status'].'"]';
		break;
	case 'install':
		$start_time = microtime(true);
		$install_pages = array();
		$install_pages[3] = 'REQUIREMENT';
		$install_pages[4] = 'FILE';
		$install_pages[5] = 'TABLE';
		$install_pages[6] = 'SCRIPT';
		if($_SESSION['current_user']->isRoot() && isset($_SESSION['install']['cancel']) && !$_SESSION['install']['cancel'])
		{
			if(!isset($_SESSION['install']['id']) || !isset($_GET['id']) || $_SESSION['install']['id'] != $_GET['id'])
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_ID»");';
			}
			elseif(!isset($_SESSION['install']['packages']))
			{
				echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_NOPACKAGE»");';
			}
			else
			{
				if(!isset($_SESSION['install']['screen'])) 
				{
					$_SESSION['install']['screen'] = 0;
				}
				include($project_path.'/view/action/admin/install_'.intval($_SESSION['install']['screen']).'.php5');
			}
		}
		break;
	case 'perform':
		$temp = $project_path.'/temp/';
		$install = true;
		if(file_exists($temp.'/installing.php5'))
		{
			$install = false;
			if(isset($_SESSION['install']['id']))
			{
				$pid = file_get_contents($temp.'/installing.php5');
				if($pid != $_SESSION['install']['id'])
				{
					if((time() - filemtime($temp.'/installing.php5'))<300)
					{
						echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_OTHERINSTALL»");
disabledForm(document.getElementById("update_form"), false)';
					}
					else
					{
						$install = true;
					}
				}
				else
				{
					$install = true;
				}
			}
			else
			{
				if((time() - filemtime($temp.'/installing.php5'))<300)
				{
					echo 'updateDisplayError("«!ADMIN_INSTALL_ERROR_OTHERINSTALL»");
disabledForm(document.getElementById("update_form"), false)';
				}
				else
				{
					$install = true;
				}
			}
		}
		if($install)
		{
			$_SESSION['install']['id'] = uniqid();
			file_put_contents($temp.'/installing.php5', $_SESSION['install']['id']);
			if($_SESSION['current_user']->isRoot())
			{
				$to_install = array();
				foreach($_POST as $name => $value)
				{
					switch($value)
					{
						case 'on':
							$action=1;
							break;
						case 'time':
							$action=2;
							break;
						default:
							$action=0;
							break;
					}
					$appli = substr($name, 13);
					if(isset($conf['appli']['packages'][$appli]))
					{
						foreach($_SESSION['install']['packages'] as $package)
						{
							if($package['name'] == $appli)
							{
								if($package['new_version'] == $conf['appli']['packages'][$appli]['update_version'] && $package['old_version'] == $conf['appli']['packages'][$appli]['version'])
								{
									$conf['appli']['packages'][$appli]['update_action'] = $action;
									if($action == 1)
									{
										$to_install[] = $package;
									}
								}
							}
						}
					}
				}
				fwTools::saveConfig('appli');
				$_SESSION['install']['packages'] = $to_install;
				
				if(count($to_install)>0)
				{
					include_once($project_path.'/class/fw/smarty.class.php5');
					$smarty = new fwSmarty();
					$_SESSION['install']['counter'] = array();
					$_SESSION['install']['screen'] = 0;
					$_SESSION['install']['cancel'] = false;
					echo 'update_popup.showModal("'.fwConverter::text2JSString($smarty->getContents('admin/install.tpl')).'");
updatePerform("'.$_SESSION['install']['id'].'");
';				
				}
				else
				{
					echo 'update_popup.hide();
update_popup=null;';
				}
			}
		}
		break;
	case 'cancel':
		$temp = $project_path.'/temp/';
		if(file_exists($temp.'/installing.php5'))
		{
			if(isset($_SESSION['install']['id']))
			{
				$pid = file_get_contents($temp.'/installing.php5');
				if($pid != $_SESSION['install']['id'])
				{
					if((time() - filemtime($temp.'/installing.php5'))>300)
					{
						@unlink($temp.'/installing.php5');
					}
				}
				else
				{
					@unlink($temp.'/installing.php5');
				}
			}
		}
		$_SESSION['install']['cancel'] = true;
		echo 'update_popup.hide();';
		break;
}
?>